//
//  myCollectionVC.m
//  NewMyMusicAPP
//
//  Created by 11_wyq on 15/6/27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "myCollectionVC.h"

@interface myCollectionVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation myCollectionVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
 //   self.hidesBottomBarWhenPushed = YES;//隐藏下方Bar
    self.navigationController.navigationBar.translucent = NO;//取消navigationController透明度

    
    //       给导航栏上放自定义label
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  WIDTH * 100, HEIGHT * 30)];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 20, 0, WIDTH * 100, HEIGHT * 30)];
    title.text = @"我的收藏";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    
    
    UIImage *image2 = [UIImage imageNamed:@"menu.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(locker)];
    

    
    self.bigArray = [NSMutableArray arrayWithObjects:@"MV",@"FM", nil];
    
    
    

    self.myTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH, MYHEIGHT) style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [[DataHandle shareHandleController] openDB];
    self.array = [[DataHandle shareHandleController] selectCollect];
    self.FMArray = [[DataHandle shareHandleController]selectMusicFMCollection];
    
    self.myTableView.showsVerticalScrollIndicator = NO;//取消纵向滑条

    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell之间的线
    self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
    
    
    
    

}






//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


//编辑类型:删除或者插入
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // [tableView setEditing:YES animated:YES];
    return UITableViewCellEditingStyleDelete;
}

//实现编辑逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  // [tableView setEditing:NO animated:YES];
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
       
        
    if (indexPath.section == 0) {
        [[DataHandle shareHandleController]openDB];
        CollectModel *model = [[[DataHandle shareHandleController]selectCollect] objectAtIndex:indexPath.row];
        [self.array removeObjectAtIndex:indexPath.row];
        [[DataHandle shareHandleController]deleteTable:model.modelId];
        //删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
        if (self.array.count == 0) {
            //删除section
            
         
            
       
        }
    }
    if (indexPath.section == 1) {
        [[DataHandle shareHandleController]openDB];
        FMPersonModel *model = [[[DataHandle shareHandleController]selectMusicFMCollection] objectAtIndex:indexPath.row];
        [[DataHandle shareHandleController]deleteMusicFMCollection:model.title];
        [self.FMArray removeObjectAtIndex:indexPath.row];
        //删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
        if (self.FMArray.count == 0) {
            //删除section
            [self.bigArray removeObjectAtIndex:indexPath.section];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
    
      
    }

  
    
}





//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"------%d",[DataHandle shareHandleController].monitorNetwork);
    //监听当前网络  有或无
    if ([DataHandle shareHandleController].monitorNetwork == YES) {
        NSLog(@"1111111111");
        
        
        
        if (indexPath.section == 0) {
            [[DataHandle shareHandleController] openDB];
            NSMutableArray *array = [[DataHandle shareHandleController] selectCollect];
            VideoPlayerViewController *videoPlayer = [[[VideoPlayerViewController alloc] init]autorelease];
            CollectModel *collect = [array objectAtIndex:indexPath.row];
            videoPlayer.Id = collect.modelId;
            videoPlayer.type = collect.type;
            
            
            [self.navigationController pushViewController:videoPlayer animated:YES];
        }
        
        
        if (indexPath.section == 1) {
            FMPlayerVC *player = [FMPlayerVC shareHandleController];
            player.indexPath = indexPath;
            FMPersonModel *fmplayer = [self.FMArray objectAtIndex:indexPath.row];
            player.title = fmplayer.title;
            NSLog(@"fmplayer = %@",fmplayer.playUrl32);
            player.trackId = fmplayer.trackId;
            player.Audio = [NSMutableArray arrayWithArray:self.FMArray];
            [self.navigationController pushViewController:player animated:YES];
            return;
        }

    }else
    {
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [aler show];
    
    }
    
    
}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    
    return self.bigArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return self.FMArray.count;
    }
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"cell";
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[CollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    if (indexPath.section == 0) {
        CollectModel *collect = [[CollectModel alloc] init];
        
        collect = [self.array objectAtIndex:indexPath.row];
        
        cell.collect = collect;
    }
    if (indexPath.section == 1) {
        
        FMPlayerModel *player = [self.FMArray objectAtIndex:indexPath.row];
        
        cell.player = player;
    
        
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
    return cell;
}




-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.bigArray.count == 0) {
        return  @" ";
    }
    

    if (section == 1) {
        return @"音乐FM";
    }

   
    return @"MV";
}




-(void)locker
{
    //抽屉
    [self.sideMenuViewController presentLeftMenuViewController];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
